﻿using ShanNiang.Service.Constants;
using ShanNiang.Service;
using System.Windows;
using System.Windows.Controls;
using Serilog;
using ShanNiang.Hosting.Enumerate;
using System.Web;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using ShanNiang.Service.Interfaces;
using ShanNiang.Service.LoginModels.Requests;
using ShanNiang.Mvvm.ViewModels;
using System.Runtime.Versioning;
using System.Windows.Threading;
using ShanNiang.Service.Attributes;
using HandyControl.Controls;

namespace ShanNiang.Packing.UserControls
{
    /// <summary>
    /// LoginControl.xaml 的交互逻辑
    /// </summary>
    [MenuControl(CommonData.MENU_LOGIN, "登录", 0, CommonData.MENU_ICON_LOGOUT)]
    [SupportedOSPlatform("windows")]
    public partial class LoginControl : UserControl
    {
        private LoginViewModel viewModel => (LoginViewModel)DataContext;
        private readonly PackingConfig _packingConfig;
        public LoginControl(LoginViewModel viewModel, PackingConfig packingConfig)
        {
            InitializeComponent();
            DataContext = viewModel;
            _packingConfig = packingConfig;
        }

        private void WebView2_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
        {
            var wv2_wxLogin = (WebView2?)sender;
            if (wv2_wxLogin != null)
            {
                //Logger.LogDebug($"浏览器地址切换{e.Uri}");
                //var url = e.Uri;
                Log.Logger.Debug(e.Uri);
                if (e.Uri.Contains(CommonData.OriginalIdentityServer) && e.Uri.Contains(CommonData.WeChatCallback))
                {
                    var url = e.Uri.Replace(CommonData.OriginalIdentityServer, _packingConfig.IdentityServer);
                    var uri = new Uri(url);
                    //var wechatCallback = $"{uri.Scheme}://{uri.Host}:{uri.Port}{CommonData.WeChatCallback}{uri.Query}{uri.Fragment}";
                    var query = uri.Query.Substring(1);
                    string code = string.Empty;
                    string state = string.Empty;
                    if (!string.IsNullOrEmpty(query))
                    {
                        var arr = query.Split('&');
                        foreach (var item in arr)
                        {
                            if (item.ToLower().StartsWith("code"))
                            {
                                var para = item.Split("=");
                                if (para.Length == 2)
                                {
                                    code = para[1];
                                }
                            }
                            if (item.ToLower().StartsWith("state"))
                            {
                                var para = item.Split('=');
                                if (para.Length == 2)
                                {
                                    state = para[1];
                                }
                            }
                        }
                    }
                    viewModel.GetTokenCommand.Execute(new WxLoginRequest(code, state));
                    wv2_wxLogin.Source = new Uri(url);
                }
            }
        }
    }
}
